package com.yongchun.config.adapay;


import com.huifu.adapay.model.MerConfig;
import com.yongchun.commons.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2022-09-27 21:14
 */
@Component
public class AdapayUtil {


    @Autowired
    private AdapayListConfig adapayListConfig;


    public AdapayItemConfig getOneAdapayConfigByAppId(String appId) throws Exception {
        if (StringUtil.isNullOrEmpty(adapayListConfig) || StringUtil.isNullOrEmpty(adapayListConfig.getList())) {
            throw new Exception("支付配置有误");
        }
        int size = adapayListConfig.getList().size();

        if (size == 0) {
            throw new Exception("支付配置有误");
        }
        AtomicReference<AdapayItemConfig> configAtomicReference = new AtomicReference<AdapayItemConfig>();


        adapayListConfig.getList().stream().forEach(adapayItemConfig -> {
            if (StringUtil.equal(appId, adapayItemConfig.getWxAppId())) {
                configAtomicReference.set(adapayItemConfig);
                return;

            }
        });
        AdapayItemConfig config = configAtomicReference.get();
        initMerConfig(config);

        return config;
    }

    public AdapayItemConfig getOneAdapayConfigByPayKey(String payKey) throws Exception {
        if (StringUtil.isNullOrEmpty(adapayListConfig) || StringUtil.isNullOrEmpty(adapayListConfig.getList())) {
            throw new Exception("支付配置有误");
        }
        int size = adapayListConfig.getList().size();
        int index = 0;
        for (int i = 0; i < size; i++) {
            if (payKey.equalsIgnoreCase(adapayListConfig.getList().get(i).getPayKey())) {
                index = i;
                break;
            }
        }


        AdapayItemConfig config = adapayListConfig.getList().get(index);
        config.setIndex(index);

        initMerConfig(config);

        return config;
    }

    public AdapayItemConfig getOneAdapayConfigByIndex(int index) throws Exception {
        if (StringUtil.isNullOrEmpty(adapayListConfig) || StringUtil.isNullOrEmpty(adapayListConfig.getList())) {
            throw new Exception("支付配置有误");
        }
        int size = adapayListConfig.getList().size();

        if (index > size) {
            throw new Exception("支付配置有误");
        }
        AdapayItemConfig config = adapayListConfig.getList().get(index);
        initMerConfig(config);
        return config;


    }


    /**
     * 随机取一套
     *
     * @return
     */
    public AdapayItemConfig getOneAdapayConfig() throws Exception {
        if (StringUtil.isNullOrEmpty(adapayListConfig) || StringUtil.isNullOrEmpty(adapayListConfig.getList())) {
            throw new Exception("支付配置有误");
        }
        int size = adapayListConfig.getList().size();

        int index = (int) (Math.random() * size);


        AdapayItemConfig config = adapayListConfig.getList().get(index);
        config.setIndex(index);

        initMerConfig(config);

        return config;

    }


    /**
     * @param config
     * @return
     * @throws Exception
     */
    public MerConfig initMerConfig(AdapayItemConfig config) throws Exception {
        return  null;
        /*

        MerConfig merConfig = new MerConfig();
        merConfig.setApiKey(config.getApiKey());
        merConfig.setApiMockKey(config.getMockApiKey());
        merConfig.setRSAPrivateKey(config.getRsaPrivateKey());
        Adapay.initWithMerConfig(merConfig);
        return merConfig;
*/
    }


}
